package com.zryc.shop.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.base.Result;
import com.zryc.shop.pms.entity.SkuStock;
import com.zryc.shop.pms.mapper.SkuStockMapper;
import com.zryc.shop.pms.service.ISkuStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * sku的库存 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@Service
public class SkuStockServiceImpl extends ServiceImpl<SkuStockMapper, SkuStock> implements ISkuStockService {

    /**
     * 查询某个商品的SKU库存列表
     *
     * @param pid
     * @param keyword
     * @return
     */
    @Override
    public List<SkuStock> getByProductId(Long pid, String keyword) {
        QueryWrapper<SkuStock> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",pid);
        if(!StringUtils.isEmpty(keyword)){
            wrapper.eq("sku_code",keyword);
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 更新SKU库存量
     *
     * @param pid
     * @param skuStockList
     * @return
     */
    @Override
    public Object updateSkuStock(Long pid, List<SkuStock> skuStockList) {
        if (pid == null || skuStockList == null || skuStockList.isEmpty()) {
            return Result.error(666, "错误");
        }

        for (SkuStock skuStock : skuStockList) {
            if (skuStock.getProductId() == null || !skuStock.getProductId().equals(pid)) {
                return Result.error(666, "错误");
            }
            baseMapper.updateById(skuStock);
        }
        return "更新成功";
    }
}
